package ui.patient_registration.models;

import patient.Patient;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class PatientTableModel extends AbstractTableModel {
    private String[] columnNames = new String[]{"Imię i nazwisko", "Płeć", "Ubezpieczony", "Zawód"};
    private List<Patient> patients;
    private List<Patient> filteredPatients;

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    public PatientTableModel(List<Patient> patients) {
        super();
        if (patients != null) {
            this.patients = patients;
            this.filteredPatients = new ArrayList<Patient>();
            refilter();

            System.out.println(patients);
        } else {
            this.patients = new ArrayList<Patient>();
            this.filteredPatients = new ArrayList<Patient>();
        }
        sortPatients();
    }

    @Override
    public int getRowCount() {
        return filteredPatients.size();
    }

    public void addPatient(Patient patient) {
        patients.add(patient);
        sortPatients();
        refilter();
    }

    public void sortPatients() {
        Comparator<Patient> comparator = new Comparator<Patient>() {
            @Override
            public int compare(Patient pat1, Patient pat2) {
                return pat1.getImie().compareToIgnoreCase(pat2.getImie());
            }
        };

        Collections.sort(patients, comparator);
        Collections.sort(filteredPatients, comparator);
        fireTableDataChanged();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Patient patient = filteredPatients.get(rowIndex);

        switch (columnIndex) {
            case 0:
                return patient.getImie() + " " + patient.getNazwisko();
            case 1:
                return patient.getPlec().toString().charAt(0);
            case 2:
                return patient.isCzyUbezpieczony();
            case 3:
                return patient.getZawod();
            default:
                return "";
        }

    }

    public Patient getPatientByRow(int rowIndex) {
        return filteredPatients.get(rowIndex);
    }

    public void editPatientByRow(Patient newPatient, int rowIndex) {
        patients.set(rowIndex, newPatient);
        refilter();
        sortPatients();
        fireTableDataChanged();
    }

    public void removeRow(int row) {
        patients.remove(row);
        filteredPatients.remove(row);
        sortPatients();
        fireTableRowsDeleted(row, row);
    }

    private void refilter() {
        filteredPatients.clear();
        for (Patient patient : patients)
            filteredPatients.add(patient);
        fireTableDataChanged();
    }

    public void refilter(String imie, String nazwisko) {
        filteredPatients.clear();

        for (Patient patient : patients) {

            if (patient.getImie().toLowerCase().contains(imie.toLowerCase())
                    || patient.getNazwisko().toLowerCase().contains(nazwisko.toLowerCase()))
                filteredPatients.add(patient);
        }

        fireTableDataChanged();
    }

    public void resetFilter() {
        filteredPatients.clear();
        for (Patient patient : patients)
            filteredPatients.add(patient);
        fireTableDataChanged();
    }
}
